import 'package:flutter/material.dart';
import 'package:flutter_09_game/base/controller/base_controller.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/pages/home/home_gameplay_tutorial/home_gameplay_tutorial_list/home_gameplay_tutorial_list_view.dart';
import 'package:flutter_09_game/pages/home/home_gameplay_tutorial/home_gameplay_tutorial_list/model_game_active.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/widgets/tab/common_tab_bar_page.dart';
import 'package:get/get.dart';

class HomeGameplayTutorialLogic extends BaseController
    with GetSingleTickerProviderStateMixin {
  late TabController tabController;
  List<CommonTabModel> tabData = [
    // CommonTabModel(
    //   view: HomeGameplayTutorialListPage(
    //     type: LocaleKeys.lastDigitOddEven.tr,
    //   ),
    //   title: LocaleKeys.lastDigitOddEven.tr,
    // ),
    // CommonTabModel(
    //   view: HomeGameplayTutorialListPage(
    //     type: LocaleKeys.oneMinOddEven.tr,
    //   ),
    //   title: LocaleKeys.oneMinOddEven.tr,
    // ),
    // CommonTabModel(
    //   view: HomeGameplayTutorialListPage(
    //     type: LocaleKeys.threeMinOddEven.tr,
    //   ),
    //   title: LocaleKeys.threeMinOddEven.tr,
    // ),
  ];

  RxList<ModelGameActive> gameActiveList = RxList();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    // tabController = TabController(length: tabData.length, vsync: this);
    // getGameActive();
    executeInitMethod(getGameActive);
  }

  Future getGameActive() async {
    HttpResponse response = await HttpManager().get(
      API.getGameActive,
      fromJson: ModelGameActive.fromJson,
    );
    if (response.isSuccess) {
      List<ModelGameActive> games = response.data;
      List<ModelGameActive> t = [];

      addDatas(t, 1, games);
      addDatas(t, 7, games);
      addDatas(t, 9, games);
      addDatas(t, 2, games);
      addDatas(t, 3, games);

      gameActiveList.value = t;

      gameActiveList.addAll(games.where((item) =>
          item.game?.id != 1 &&
          item.game?.id != 7 &&
          item.game?.id != 9 &&
          item.game?.id != 2 &&
          item.game?.id != 3));

      // gameActiveList.value = (response.data as List<ModelGameActive>)
      //   ..sort((a, b) => (a.game?.id ?? 0).compareTo(b.game?.id ?? 0));

      for (var item in gameActiveList) {
        tabData.add(CommonTabModel(
            view: HomeGameplayTutorialListPage(
              type: (item.game?.id ?? '').toString(),
              gameActive: item,
            ),
            title: item.game?.name ?? ''));
      }
      tabController = TabController(length: tabData.length, vsync: this);
    }
  }

  addDatas(List<ModelGameActive> t, int id, List<ModelGameActive> games) {
    ModelGameActive? data =
        games.firstWhereOrNull((item) => item.game?.id == id);
    if (data != null) {
      // if (id == 7) {
      //   data.game?.name = '6秒单双';
      // } else if (id == 9) {
      //   data.game?.name = '9秒单双';
      // }
      t.add(data);
    }
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  @override
  void onClose() {
    tabController.dispose();
    // TODO: implement onClose
    super.onClose();
  }
}
